#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <amqp.h>
#include <amqp_tcp_socket.h>


//声明exchange的低级别的封装函数
void declare_exchange(amqp_connection_state_t conn) {
    amqp_exchange_declare_t req;
    req.ticket = 0;
    req.exchange = amqp_cstring_bytes("my_exchange");
    req.type = amqp_cstring_bytes("direct");
    req.passive = 0;
    req.durable = 1;        // 持久化
    req.auto_delete = 0;    // 不自动删除
    req.internal = 0;       // 非内部
    req.nowait = 0;         // 等待确认
    req.arguments = amqp_empty_table;
    
    amqp_method_number_t method_ok = AMQP_EXCHANGE_DECLARE_OK_METHOD;
    amqp_rpc_reply_t reply = amqp_simple_rpc(
        conn, 
        1, 
        AMQP_EXCHANGE_DECLARE_METHOD, 
        &method_ok, 
        &req
    );
    
    if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
        fprintf(stderr, "Failed to declare exchange\n");
        return;
    }
    
    printf("Exchange declared successfully\n");
}

void publish_message(amqp_connection_state_t conn, const char* exchange_name, 
                     const char* routing_key, const char* message_body) {
    
    amqp_basic_properties_t props;
    props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
    props.content_type = amqp_cstring_bytes("text/plain");
    props.delivery_mode = 2; // 持久化消息
    
    int status = amqp_basic_publish(
        conn,
        1,  // channel
        amqp_cstring_bytes(exchange_name),
        amqp_cstring_bytes(routing_key),
        0,  // mandatory
        0,  // immediate
        &props,
        amqp_cstring_bytes(message_body)
    );
    
    if (status != AMQP_STATUS_OK) {
        fprintf(stderr, "Failed to publish message\n");
        return;
    }
    
    printf("Message published: %s\n", message_body);
}

int main() {
    amqp_connection_state_t conn;
    amqp_socket_t *socket;
    
    // 建立连接
    conn = amqp_new_connection();
    socket = amqp_tcp_socket_new(conn);
    
    if (amqp_socket_open(socket, "localhost", 5672) != AMQP_STATUS_OK) {
        fprintf(stderr, "Failed to open socket\n");
        return 1;
    }
    
    // 登录
    if (amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, 
                   "guest", "guest").reply_type != AMQP_RESPONSE_NORMAL) {
        fprintf(stderr, "Failed to login\n");
        return 1;
    }
    
    // 打开通道
    amqp_channel_open(conn, 1);
    if (amqp_get_rpc_reply(conn).reply_type != AMQP_RESPONSE_NORMAL) {
        fprintf(stderr, "Failed to open channel\n");
        return 1;
    }
    
    // 声明 exchange
    amqp_exchange_declare(conn, 1, amqp_cstring_bytes("test_exchange"),
                         amqp_cstring_bytes("direct"), 0, 1, 0, 0,
                         amqp_empty_table);
    if (amqp_get_rpc_reply(conn).reply_type != AMQP_RESPONSE_NORMAL) {
        fprintf(stderr, "Failed to declare exchange\n");
        return 1;
    }
    
    // 发布消息
    amqp_basic_properties_t props;
    props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG;
    props.content_type = amqp_cstring_bytes("text/plain");
    
    const char *message = "Hello from C client!";
    amqp_basic_publish(conn, 1, amqp_cstring_bytes("test_exchange"),
                      amqp_cstring_bytes("test_key"), 0, 0,
                      &props, amqp_cstring_bytes(message));
    
    if (amqp_get_rpc_reply(conn).reply_type != AMQP_RESPONSE_NORMAL) {
        fprintf(stderr, "Failed to publish message\n");
        return 1;
    }
    
    printf("Message sent: %s\n", message);
    
    // 清理连接
    amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);
    amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
    amqp_destroy_connection(conn);
    
    return 0;
}
